package com.giaothong;

import java.util.Collection;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;

import com.giaothong.entities.Choice;
import com.giaothong.entities.Question;
import com.giaothong.view.BienBaoGridAdapter;

public class ShowGridBienBaoActivity extends Activity {

	
	private GridView gridViewBienBao;
	
	
	private BienBaoGridAdapter bBaoGridAdapter;
	Context context;
	private Button btnSetting;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.screen_hienthibienbao_gridview);
		 
		context = this;
		viewUnit();
		
		
		bBaoGridAdapter = new BienBaoGridAdapter(context, TabLyThuyetActivity.bBaoList);
		gridViewBienBao.setAdapter(bBaoGridAdapter);
	
		gridViewBienBao.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View arg1, int position,
					long id) {
				// TODO Auto-generated method stub
				Log.i("position = ",position +"");
				Log.i("tilte = ",TabLyThuyetActivity.bBaoList.get(position).getTitle().toString());
				createDialog(position);
				
			}
		}); 
		
		btnSetting.setOnClickListener(new OnClickListener() {		
			@Override 
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent inten = new Intent(context, ShowListBienBaoActivity.class);
				startActivity(inten);
				onPause();
			}
		});	
	}
	private void viewUnit(){
		gridViewBienBao = (GridView)findViewById(R.id.bienbao_isgridview);
		btnSetting = (Button)findViewById(R.id.hienBienBao_btn_setting);
	}
	
	private void createDialog(int position){
		Question question = TabLyThuyetActivity.bBaoList.get(position);
		
		AlertDialog.Builder alert = new AlertDialog.Builder(context);
		
		alert.setTitle(question.getTitle()+"");
		
//		int idPatchQuestion = context.getResources().getIdentifier(question.getImage().trim(), "drawable", context.getPackageName());
//		if(idPatchQuestion > 0){
//			
//			alert.setIcon(idPatchQuestion);
//		}
		
		Collection<Choice> choice= question.getChoices();
		for(Choice c : choice){
			alert.setMessage(c.getTitle());
		}
		alert.setNegativeButton(getResources().getString(R.string.dong),new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog,int which) {
				// xu li neu nguoi dung chon NO
				dialog.cancel();
			}
		});
		alert.create();
		alert.show();
	}
}
